package com.woniuxy.woniubook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.woniubook.entity.Book;
import com.woniuxy.woniubook.mapper.BookMapper;
import com.woniuxy.woniubook.service.BookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wugd
 * @since 2022-11-16 02:13:11
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Autowired
    BookMapper bookMapper ;

    @Override
    public List<Book> getBookByTopTen() {
        Page<Book> page = new Page<>(1,12);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .orderByDesc("buycount");
        Page<Book> bookPage = bookMapper.selectPage(page, queryWrapper);
        return bookPage.getRecords();
    }

    @Override
    @Cacheable(value = "book",key = "#root.target+#root.methodName+#p0+#p1+#p2+#p3")
    public Page<Book> getBookByPageAndTypeId(Integer now, Integer size, Integer tid, String searchName) {
        Page<Book> page = new Page<>(now,size);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        if (searchName != null && !"".equals(searchName.trim())){
            queryWrapper.like("name",searchName);
        }
        queryWrapper.eq("typeId",tid);

        Page<Book> page1 = bookMapper.selectPage(page, queryWrapper);
        return page1;
    }
}
